<?php
namespace Admin\Controller;
use Org\Util\Category;
class CategoryController extends AdminController {
    public function _initialize()
    {
        parent::_initialize();
        $this->model = D('Category');
    }
    public function index(){
        $this->_web('title', '分类管理');
        $this->_web('panel_title', '分类管理');
        $this->IS_POST(function (){
            $cate = new Category(D('Category'), array('id', 'pid', 'title'), '', 'sort desc');
            $map = array();
            if(I('keyword')) {
                $kw = '%'.str_replace(' ', '%', I('keyword')).'%';
                $map['name|title|keywords|meta_title|id'] = array('like', $kw);
            }
            $list = $cate->getList($map);
            $data = array(
                'code' => 0,
                'count' => count($list),
                'data' => $list
            );
            $this->ajaxReturn($data);
        });
        $this->_web('title', '分类管理');
        $this->_web('panel_title', '分类管理');
        $listData = array(
            'url' => U(''),
            'toolbar' => '#barDemo',
            'toolbarTpl' => 'toolbar',
            'page' => false,
        );
        $listData['cols'][] = array('type' => 'checkbox', 'fixed' => 'left');
        $listData['cols'][] = array('field' => 'id', 'title' => 'ID', 'sort' => true);
        $listData['cols'][] = array('field' => 'icon_html','align'=>'center', 'title' => '图标','width'=>60);
        $listData['cols'][] = array('field' => 'fullname','align'=>'left', 'title' => '分类标题','width'=>'20%');
        $listData['cols'][] = array('field' => 'name', 'title' => '分类标识', 'width' => '10%');
        $listData['cols'][] = array('field' => 'status_text', 'title' => '状态', 'width' => '10%');
        $listData['cols'][] = array('field' => 'sort', 'title' => '排序');
        $listData['cols'][] = array('fixed' => 'right','width'=>'220', 'title' => '操作', 'toolbar'=>'#rowBar');
        $this->displayList($listData);
    }

    public function add() {
        $id = I('id', '', 'intval');
        $model = D('Category');
        if (IS_POST) {
            $data = $model->create();
            if (!$data) {
                $this->error($model->getError());
            } else if ($model->in2replace($data, array('id'=>$id)) !== false) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
            return true;
        }
        $cate = new Category($model, array('id', 'pid', 'title'), '', 'sort desc');
        $pid = I('pid');
        $listMap = array();
        if ($id) {
            $info = $model->find($id);
            $listMap['id'] = array('neq', $info['id']);
            $this->assign('info', $info);
        }
        if ($pid) {
            $parent = $model->where(array(
                'id' => $pid,
            ))->field('id,title')->find();
            $this->assign('parent', $parent);
        } else {
            $list = $cate->getList($listMap);
            $this->assign('list', $list);
        }
        $this->display();
    }
    public function view($id) {
        $info = $this->model->find($id);
        if(!$info) {
            $this->error('分类不存在');
        }
        $this->assign('info', $info);
        $this->display();
    }
    public function _beging_del($ids, &$url) {
        $url = '';
    }
}